package com.bing.service.impl;

import com.bing.entity.LoanProducts.LoanProductinfo;
import com.bing.entity.Statistical.*;
import com.bing.mapper.LoanProdductsinfoMapper;
import com.bing.mapper.StatistcalMapper;
import com.bing.service.StatisticalService;
import com.bing.utils.idgenertor.IdGen;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Mr.HRL
 * @Description:
 * @Date: 2019-09-14 15:56
 * @Version: 1.0
 */
@Service
public class StatisticalServiceImpl implements StatisticalService {
    @Autowired(required = false)
    StatistcalMapper statistcalMapper;
    @Autowired
    private LoanProdductsinfoMapper loanProdductsinfoMapper;

    private Integer PAGE_SIZE = 15;
    /**
     * 产品访问记录
     * @param productAccessInfo
     */
    @Override
    public void productAccessAdd(ProductAccessInfo productAccessInfo) {
        String bpId = productAccessInfo.getBpId();
        String productId = productAccessInfo.getProductId();
        int num = statistcalMapper.getClickBybpId(bpId);
        int nump = statistcalMapper.getClickByProductId(productId);
        if (num==0){
            productAccessInfo.setBpClickNum(1);
        } else {
            productAccessInfo.setBpClickNum(num+1);
        }
        if (nump==0){
            productAccessInfo.setProClickNum(1);
        } else {
            productAccessInfo.setProClickNum(nump+1);
        }
        productAccessInfo.setId(IdGen.uuid());
        productAccessInfo.setClicktime(new Date());
        statistcalMapper.productAccessAdd(productAccessInfo);
    }


    /**
     * 渠道访问记录
     * @param channelAccessInfo
     */
    @Override
    public String channelAccessAdd(ChannelAccessInfo channelAccessInfo) throws Exception {
        String  channelId = channelAccessInfo.getChannlId();
        if (StringUtils.isEmpty(channelId)){
            throw new Exception("渠道不能为空");
        }
        String eqId =  null;
        if (StringUtils.isEmpty(channelAccessInfo.getEqId())){
            eqId = IdGen.uuid();
        } else {
            eqId = channelAccessInfo.getEqId();
        }

        int num = statistcalMapper.getClickByEqtId(eqId);
        int nump = statistcalMapper.getClickByChannelId(channelId);
        if (num==0){
            channelAccessInfo.setEqClickNum(1);
        } else {
            channelAccessInfo.setEqClickNum(num+1);
        }
        if (nump==0){
            channelAccessInfo.setChanClickNum(1);
        } else {
            channelAccessInfo.setChanClickNum(nump+1);
        }
        channelAccessInfo.setId(IdGen.uuid());
        channelAccessInfo.setEqId(eqId);
        channelAccessInfo.setClicktime(new Date());
        statistcalMapper.channelAccessAdd(channelAccessInfo);
        return eqId;
    }



    /**
     * 预计注册 该产品
     * @param expectRegistereInfo
     */
    @Override
    public void exceptRegAdd(ExpectRegistereInfo expectRegistereInfo) {
        String bpId = expectRegistereInfo.getBpId();
        List<String> list = statistcalMapper.checkBpProdct(bpId);
        if (list!=null&&list.size()>0) {
            if (!list.contains(expectRegistereInfo.getProductId())) {
                expectRegistereInfo.setId(IdGen.uuid());
                expectRegistereInfo.setEsRegTime(new Date());
                statistcalMapper.exceptRegAdd(expectRegistereInfo);
            }
        }

    }

    /**
     * 产品已经申请人数  产品上报接口第一次进来+1   第二次不在累加
     */
    @Override
    public void sumClick(ProductAccessInfo productAccessInfo) {
        List<ProductAccessInfo> productAccess = statistcalMapper.getProductAccess(productAccessInfo.getBpId(), productAccessInfo.getProductId());
        if (!CollectionUtils.isNotEmpty(productAccess)){
            LoanProductinfo productDetail = loanProdductsinfoMapper.getProductDetail(productAccessInfo.getProductId());
            productDetail.fill();
            loanProdductsinfoMapper.updateByPrimaryKeySelective(productDetail);
        }
    }

    /**
     * 同一用户id  产品id 不可重复
     */
    @Override
    public void checkBpProdct(ExpectRegistereInfo expectRegistereInfo) {

    }

    /**
     * 用户统计
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map<Object, Object> userStatis(Date startTime, Date endTime) {
        List<Map<Object,Object>>  maps = statistcalMapper.getListByzc(startTime,endTime);
        List<Map<Object,Object>>  maps1 = statistcalMapper.getListByyjzc(startTime,endTime);
        Map<Object,Object> map = new HashMap<>();
        map.put("normal",maps);
        map.put("expect",maps1);
        return map;
    }

    /**
     * 产品统计
     * @param startTime
     * @param endTime
     * @param productName
     * @return
     */
    @Override
    public  List<ProductStatisinfo> productStatis(Date startTime, Date endTime, String productName) {
        List<ProductStatisinfo>  productStatisinfos = statistcalMapper.productStatis(startTime,endTime,productName);
        return productStatisinfos;
    }

    /**
     * 渠道统计
     * @param startTime
     * @param endTime
     * @param channelName
     * @return
     */
    @Override
    public List<ChannelStatisinfo> channelStatis(Date startTime, Date endTime, String channelName) {
        List<ChannelStatisinfo>  channelStatisinfos = statistcalMapper.channelStatis(startTime,endTime,channelName);
        return channelStatisinfos;
    }



    /**
     * 甲方数据录入
     * @param partyStatisinfo
     */
    @Override
    public void addPartyData(PartyStatisinfo partyStatisinfo) {
       /*    String productId = partyStatisinfo.getProductId();
             if (StringUtils.isNotEmpty(productId)) {
             String data = statistcalMapper.getPartyDataByProduct(productId);
             if (StringUtils.isNotEmpty(data)) {
                 statistcalMapper.updatePartyData(partyStatisinfo);
             } else {

             }
         }*/
        partyStatisinfo.setJid(IdGen.uuid());
        statistcalMapper.addPartyData(partyStatisinfo);
    }


    //获取录入甲方产品
    @Override
    public PageInfo<PartyStatisinfo> getPartyData(String productName, Integer pageNum) {
        PageHelper.startPage(pageNum, PAGE_SIZE);
        List<PartyStatisinfo> partyData = statistcalMapper.getPartyData(productName);
        return new PageInfo<>(partyData);
    }

}
